package com.html5;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RemoteViews;

import com.phonegap.DroidGap;

public class HTML5Activity extends DroidGap {
	public  NotificationManager nm;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.loadUrl("file:///android_asset/www/index.html");
		nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		showNotification(R.drawable.logo, "天天音乐", "天天音乐", "");
	}

	public void showNotification(int icon, String tickertext, String title,
			String content) {

		// Notification管理器
		Notification notification = new Notification(icon, tickertext,System.currentTimeMillis());
		notification.flags=Notification.FLAG_ONGOING_EVENT;//该标志表示当用户点击 Clear 之后，不能够清除该通知。
		//notification.defaults = Notification.DEFAULT_ALL;
		
		
		Intent intent=new Intent(this,HTML5Activity.class);
		/**
		 * Intent.FLAG_ACTIVITY_CLEAR_TOP ：如果在当前Task中，有要启动的Activity，那么把该Acitivity之前的所有Activity都关掉，并把此Activity置前以避免创建Activity的实例
           Intent.FLAG_ACTIVITY_NEW_TASK ：系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task，若有，则在该Task上创建Activity，若没有则新建具有该Activity属性的
		 */
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent pt = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		
        RemoteViews remoteViews=new RemoteViews(HTML5Activity.this.getPackageName(),R.layout.notivication);
        remoteViews.setImageViewResource(R.id.logo, R.drawable.logo);
        remoteViews.setTextViewText(R.id.title,title);
        notification.contentView=remoteViews;
        notification.contentIntent=pt;
        
		//notification.setLatestEventInfo(this, title, content, pt);
		nm.notify(19172439, notification);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1, 0, R.string.exit);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case 1:
			exit();
			return true;
		}
		return false;
	}

	public void exit() {
		
			 AlertDialog.Builder builder = new Builder(this);
		        builder.setMessage("确定要退出吗?");
		        builder.setTitle("提示");
		        builder.setPositiveButton("确认",
		                new android.content.DialogInterface.OnClickListener() {
		                    public void onClick(DialogInterface dialog, int which) {
		                        dialog.dismiss();
		                        nm.cancel(19172439);
		                        android.os.Process.killProcess(android.os.Process.myPid());
		                    }
		                });
		        builder.setNegativeButton("取消",
		                new android.content.DialogInterface.OnClickListener() {
		                    public void onClick(DialogInterface dialog, int which) {
		                        dialog.dismiss();
		                    }
		                });
		        builder.create().show();
		       
		
	}

}